using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Triggers
{
    [Serializable]
    public class TimerTriggerOutput:ResultBase 
    {
        private DateTime _TriggerFireTime;

        public DateTime TriggerFireOn
        {
            get { return _TriggerFireTime; }
            set { _TriggerFireTime = value; }
        }
	

        public TimerTriggerOutput():base(Guid.Empty, Guid.Empty)
        {
            this._TriggerFireTime = DateTime.Now;
        }

        public TimerTriggerOutput(Guid timerTriggerComponentID, Guid jobTicketID, DateTime timestamp)
            : base(timerTriggerComponentID, jobTicketID)
        {
            this._TriggerFireTime = timestamp;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Trigger");
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"Type",this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"FinishTime",this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TriggerFireTime", this._TriggerFireTime.ToString());
            return xNode;
        }

        public override ResultBase Instantiate(XmlNode xNode)
        {
            TimerTriggerOutput output=new TimerTriggerOutput();
            string typeName = xNode.Attributes.GetNamedItem("Type").Value;
            if(typeName==this.GetType().FullName)
            {
                output.OwnerWorkerInstanceID=new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);
                
                output.TriggerFireOn = DateTime.Parse(xNode.Attributes.GetNamedItem("TriggerFireTime").Value);

                return output;
            }
            else
            {
                throw new Exception("Invalid type to deserialize from xml doc");
            }
        }
    }
}
